package com.yy.controller;

import com.yy.Utils.JsonResult;
import com.yy.pojo.Role;
import com.yy.service.RoleService;
import com.yy.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: yy
 * @Date 2021/11/14 12:52
 */
@RestController

@RequestMapping("/role")
@ResponseBody
public class RoleController extends BaseController{
    @Autowired
    RoleService roleService;

    /**
     * 查看
     */
    @GetMapping("/getRoleList")
    public List<Role> getRoleList(){
        List<Role> roleList = roleService.getRoleList();
        for (Role role : roleList){
            System.out.println(role);
        }
        return roleList;
    }


    @GetMapping("get_by_uid")
    public JsonResult<Role> roleGetByUid(HttpSession session) {
        String uid = getUidFromSession(session);
        Role data = roleService.roleGetByUid(uid);
        return new JsonResult<Role>(OK,data);
    }

    @GetMapping("get_by_uname")
    public JsonResult<Role> roleGetByUname(HttpSession session) {
        String uname = getUidFromSession(session);
        Role data = roleService.roleGetByUid(uname);
        return new JsonResult<Role>(OK,data);
    }

//    @RequestMapping("get_role_list")
//    public JsonResult<List<Role>> getRoleList() {
//        List<Role> data = roleService.getRoleList();
//        return new JsonResult<>(OK,data);
//    }

    @RequestMapping("/deleteRole/{rname}")
    public JsonResult<Void> delete(@PathVariable("rname") String rid){
        roleService.deleteRole(rid);
        return new JsonResult<Void>(OK);
    }

    @RequestMapping("addrole")
    public JsonResult<Void> addRole(Role role){
        roleService.addRole(role);
        return new JsonResult<Void>(OK);
    }
}
